<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>里氏替换原则 - 面向对象设计的基石</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .principle-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 30px;
            border-radius: 16px;
            position: relative;
            overflow: hidden;
        }
        
        .principle-card::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.3; }
        }
        
        .highlight-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .list-item {
            position: relative;
            padding-left: 30px;
            margin-bottom: 15px;
        }
        
        .list-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 8px;
            width: 8px;
            height: 8px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-10px); }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-layer-group text-6xl opacity-90"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                里氏替换原则
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                面向对象设计的基石，确保子类能够完美替代父类，构建稳定可扩展的软件架构
            </p>
            <div class="mt-8 flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-user mr-2"></i>Barbara Liskov, 1987
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-cube mr-2"></i>SOLID 原则
                </span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- Definition Card -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-book-open mr-3 text-purple-600"></i>定义
            </h2>
            <div class="text-lg text-gray-700 leading-relaxed">
                <p class="mb-4">
                    <span class="highlight-text">里氏替换原则</span> 指的是"子类对象应该能够替代父类对象出现，并且程序的行为不会受到影响。" 
                </p>
                <div class="principle-card mt-6">
                    <i class="fas fa-quote-left text-3xl opacity-50 mb-4"></i>
                    <p class="text-xl italic">
                        任何使用父类的地方都应该可以使用其子类，而不会出现错误或不符合预期的行为。
                    </p>
                </div>
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="content-card rounded-2xl p-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-exchange-alt mr-3 text-purple-600"></i>替换性
                </h3>
                <p class="text-gray-700 leading-relaxed">
                    如果一个程序的功能依赖于某个父类，那么在该程序中使用该父类的任何子类时，程序的行为应该保持一致。
                </p>
            </div>
            
            <div class="content-card rounded-2xl p-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-handshake mr-3 text-purple-600"></i>契约继承
                </h3>
                <p class="text-gray-700 leading-relaxed">
                    子类必须遵循父类的契约，即子类应满足父类定义的所有行为规范，而不能违反这些规范。
                </p>
            </div>
        </div>

        <!-- Impact on Design -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>对代码设计的影响
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <i class="fas fa-sync-alt text-3xl text-purple-600 mb-4"></i>
                    <h4 class="font-bold text-lg mb-2">增强可替换性</h4>
                    <p class="text-gray-700">确保子类能够被安全地替代父类，增强代码的灵活性和可维护性</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-purple-50 p-6 rounded-xl">
                    <i class="fas fa-expand-arrows-alt text-3xl text-blue-600 mb-4"></i>
                    <h4 class="font-bold text-lg mb-2">提高可扩展性</h4>
                    <p class="text-gray-700">子类可以扩展父类功能，而不会破坏现有代码</p>
                </div>
                <div class="bg-gradient-to-br from-pink-50 to-orange-50 p-6 rounded-xl">
                    <i class="fas fa-shield-alt text-3xl text-pink-600 mb-4"></i>
                    <h4 class="font-bold text-lg mb-2">避免错误使用</h4>
                    <p class="text-gray-700">确保子类与父类行为一致，避免不符合预期的错误</p>
                </div>
            </div>
        </div>

        <!-- Visualization -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>原则可视化
            </h2>
            <div class="mermaid">
                graph TD
                    A[里氏替换原则 LSP] --> B[核心要求]
                    B --> C[行为一致性]
                    B --> D[契约遵守]
                    B --> E[状态保持]
                    
                    C --> F[子类不改变父类预期行为]
                    D --> G[遵循接口规范]
                    E --> H[避免不一致状态]
                    
                    A --> I[设计收益]
                    I --> J[提高可维护性]
                    I --> K[增强可扩展性]
                    I --> L[减少错误风险]
                    
                    style A fill:#667eea,stroke:#764ba2,stroke-width:3px,color:#fff
                    style B fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                    style I fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Code Example -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-code mr-3 text-purple-600"></i>代码示例
            </h2>
            
            <div class="mb-6">
                <h3 class="text-xl font-bold mb-4 text-gray-800">违反 LSP 的设计</h3>
                <p class="text-gray-700 mb-4">考虑一个简单的几何形状例子：</p>
                
                <div class="code-block p-6 mb-4">
                    <pre><code>public class Rectangle {
    private int width;
    private int height;

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
}</code></pre>
                </div>

                <p class="text-gray-700 mb-4">Square 类继承自 Rectangle 类：</p>
                
                <div class="code-block p-6 mb-4">
                    <pre><code>public class Square extends Rectangle {
    @Override
    public void setWidth(int width) {
        super.setWidth(width);
        super.setHeight(width);
    }

    @Override
    public void setHeight(int height) {
        super.setHeight(height);
        super.setWidth(height);
    }
}</code></pre>
                </div>

                <div class="bg-red-50 border-l-4 border-red-500 p-4 rounded-r-lg">
                    <p class="text-red-800">
                        <i class="fas fa-exclamation-triangle mr-2"></i>
                        <strong>问题：</strong>Square 类破坏了 Rectangle 类的行为契约。Rectangle 假定宽度和高度可以独立设置，而 Square 强制它们相等。
                    </p>
                </div>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="content-card rounded-2xl p-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-check-circle mr-3 text-purple-600"></i>符合 LSP 的设计准则
            </h2>
            <div class="space-y-4">
                <div class="list-item">
                    <h4 class="font-bold text-lg mb-2">确保子类行为一致</h4>
                    <p class="text-gray-